package compte;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;


public class Dom {
	// 
	// 
	// 
	Document doc ;
	String fileName ;
	// 
	// 	
	public Dom(String filename) {
		try {
			// Create a builder factory
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
			// Create the builder and parse the file
			doc = factory.newDocumentBuilder().parse(new File(filename));
			fileName = filename ;
		} catch (ParserConfigurationException e) {
			System.out.println(e.toString());
		} catch (SAXException e) {
			System.out.println(e.toString());
		} catch (IOException e) {
			System.out.println(e.toString());
		}
	}
	// 
	// 	
	public Dom(String rootElement, String filename ) {
		try {
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			doc = dbf.newDocumentBuilder().newDocument();
			Element rootEle = doc.createElement(rootElement) ;
			doc.appendChild(rootEle) ;

			fileName = filename ;
		} catch (ParserConfigurationException e) {
			System.out.println(e.toString());
		}

	}
	// 
	// 	
	public void Save() {
		try{
			TransformerFactory tfabrique = TransformerFactory.newInstance();
			Transformer transformeur = tfabrique.newTransformer();

			Properties proprietes = new Properties();
			proprietes.put("method", "xml");
			proprietes.put("encoding", "ISO-8859-1"); 
			proprietes.put("indent", "yes");
			transformeur.setOutputProperties(proprietes);

			Source entree = new DOMSource(doc);
			File oFic = new File( fileName );
			FileOutputStream fos = new FileOutputStream(oFic);
			if(fos != null) {
				Result sortie = new StreamResult(fos);
				transformeur.transform(entree, sortie);
				fos.flush();
				fos.close();
			}
		} catch (FileNotFoundException e) {
			System.out.println(e.toString());
		} catch (FactoryConfigurationError e) {
			System.out.println(e.toString());
		} catch (TransformerException e) {
			System.out.println(e.toString());
		} catch (IOException e) {
			System.out.println(e.toString());
		}
	}
	// 
	// 	
	public void Transform(String xslFileName, String htmlFileName) {
		try{
			TransformerFactory tfabrique = TransformerFactory.newInstance();
			File XslFile = new File( xslFileName );
			Source xslSource = new StreamSource(XslFile);
			Transformer transformeur = tfabrique.newTransformer(xslSource);

			Source entree = new DOMSource(doc);
			File htmlFic = new File( htmlFileName );
			FileOutputStream fos = new FileOutputStream(htmlFic);
			if(fos != null) {
				Result sortie = new StreamResult(fos);
				transformeur.transform(entree, sortie);
				fos.flush();
				fos.close();
			}
		} catch (Exception e) {
			System.out.println("DOM.Transform : "+e.toString());
		}
	}
	// 
	//
	public void Add2Elts(String NodeName, String Elt1Name, String Elt1Value, String Elt2Name, String Elt2Value){
		try{
			Element Node = doc.createElement(NodeName); 
			Node.setAttribute(Elt1Name, Elt1Value);           
			Node.setAttribute(Elt2Name, Elt2Value);           
			doc.getDocumentElement().appendChild(Node);
		} catch (Exception e) {
			System.out.println("DOM.Add2Elts : "+e.toString());
		}

	}
	// 
	// 	
	public String [][] Get2EltsArray( String NodeName, String Elt1Name, String Elt2Name){
		try{
			NodeList nodelist  = doc.getDocumentElement().getElementsByTagName(NodeName) ;

			if(nodelist != null && nodelist.getLength() > 0) {
				String TwoEltArray [][]  = new String  [nodelist.getLength()][2]; 
				for (int i=0;i<nodelist.getLength();i++){
					Element el = (Element)nodelist.item(i);
					TwoEltArray[i][0] = el.getAttribute( Elt1Name);
					TwoEltArray[i][1] = el.getAttribute( Elt2Name);
				}
				return TwoEltArray ;
			}
		} catch (Exception e) {
			System.out.println("DOM.Get2EltsArray : "+e.toString());
		}
		return null ;
	}

	public boolean Remove2Elts( String NodeName, String Elt1Name, String Elt1Value,  String Elt2Name, String Elt2Value ){
		NodeList nodelist  = doc.getDocumentElement().getElementsByTagName(NodeName) ;
		if(nodelist != null && nodelist.getLength() > 0) {

			for (int i=0;i<nodelist.getLength();i++){
				Element el = (Element)nodelist.item(i);
				if ( el.getAttribute( Elt1Name)==Elt1Value && el.getAttribute( Elt2Name)==Elt2Value){
					doc.getDocumentElement().removeChild(el);
					doc.getDocumentElement().normalize();
					return true ;
				}

			}			
		}
		return  false;
	}

	// 
	// 	
	public void Add10Elts(String NodeName, String Elt1Name, String Elt1Value, 
			String Elt2Name, String Elt2Value,	String Elt3Name, String Elt3Value, 
			String Elt4Name, String Elt4Value,	String Elt5Name, String Elt5Value, 
			String Elt6Name, String Elt6Value,	String Elt7Name, String Elt7Value,	
			String Elt8Name, String Elt8Value,	String Elt9Name, String Elt9Value,	
			String Elt10Name, String Elt10Value){
		Element Node = doc.createElement(NodeName); 
		Node.setAttribute(Elt1Name, Elt1Value);           
		Node.setAttribute(Elt2Name, Elt2Value);           
		Node.setAttribute(Elt3Name, Elt3Value);           
		Node.setAttribute(Elt4Name, Elt4Value);           
		Node.setAttribute(Elt5Name, Elt5Value);           
		Node.setAttribute(Elt6Name, Elt6Value);           
		Node.setAttribute(Elt7Name, Elt7Value);                 
		Node.setAttribute(Elt8Name, Elt8Value);                 
		Node.setAttribute(Elt9Name, Elt9Value);                 
		Node.setAttribute(Elt10Name, Elt10Value);                 
		doc.getDocumentElement().appendChild(Node);                    
	}
	//
	//
	public String [][] Get10EltsArray( String NodeName, String Elt1Name, String Elt2Name,
			String Elt3Name, String Elt4Name,
			String Elt5Name, String Elt6Name,
			String Elt7Name, String Elt8Name,
			String Elt9Name, String Elt10Name){
		try{
			NodeList nodelist  = doc.getDocumentElement().getElementsByTagName(NodeName) ;

			if(nodelist != null && nodelist.getLength() > 0) {
				String EltArray [][]  = new String  [nodelist.getLength()][10]; 
				for (int i=0;i<nodelist.getLength();i++){
					Element el = (Element)nodelist.item(i);
					EltArray[i][0] = el.getAttribute( Elt1Name);
					EltArray[i][1] = el.getAttribute( Elt2Name);
					EltArray[i][2] = el.getAttribute( Elt3Name);
					EltArray[i][3] = el.getAttribute( Elt4Name);
					EltArray[i][4] = el.getAttribute( Elt5Name);
					EltArray[i][5] = el.getAttribute( Elt6Name);
					EltArray[i][6] = el.getAttribute( Elt7Name);
					EltArray[i][7] = el.getAttribute( Elt8Name);
					EltArray[i][8] = el.getAttribute( Elt9Name);
					EltArray[i][9] = el.getAttribute( Elt10Name);
				}
				return EltArray ;
			}
		} catch (Exception e) {
			System.out.println("DOM.Get10EltsArray : "+e.toString());
		}
		return null ;
	}
	//
	//
	public void Add16Elts(String NodeName, 
			String Elt1Name, String Elt1Value,  String Elt2Name,  String Elt2Value,	
			String Elt3Name, String Elt3Value,  String Elt4Name,  String Elt4Value,	
			String Elt5Name, String Elt5Value,  String Elt6Name,  String Elt6Value,	
			String Elt7Name, String Elt7Value,	String Elt8Name,  String Elt8Value,	
			String Elt9Name, String Elt9Value,	String Elt10Name, String Elt10Value,
			String Elt11Name,String Elt11Value, String Elt12Name, String Elt12Value,
			String Elt13Name,String Elt13Value, String Elt14Name, String Elt14Value,
			String Elt15Name,String Elt15Value, String Elt16Name, String Elt16Value){
		Element Node = doc.createElement(NodeName); 
		Node.setAttribute(Elt1Name, Elt1Value);           
		Node.setAttribute(Elt2Name, Elt2Value);           
		Node.setAttribute(Elt3Name, Elt3Value);           
		Node.setAttribute(Elt4Name, Elt4Value);           
		Node.setAttribute(Elt5Name, Elt5Value);           
		Node.setAttribute(Elt6Name, Elt6Value);           
		Node.setAttribute(Elt7Name, Elt7Value);                 
		Node.setAttribute(Elt8Name, Elt8Value);                 
		Node.setAttribute(Elt9Name, Elt9Value);                 
		Node.setAttribute(Elt10Name, Elt10Value);                 
		Node.setAttribute(Elt11Name, Elt11Value);                 
		Node.setAttribute(Elt12Name, Elt12Value);                 
		Node.setAttribute(Elt13Name, Elt13Value);                 
		Node.setAttribute(Elt14Name, Elt14Value);                 
		Node.setAttribute(Elt15Name, Elt15Value);                 
		Node.setAttribute(Elt16Name, Elt16Value);                 
		doc.getDocumentElement().appendChild(Node);                    
	}
	//
	//
	public String [][] Get16EltsArray( String NodeName, 
			String Elt1Name, String Elt2Name,
			String Elt3Name, String Elt4Name,
			String Elt5Name, String Elt6Name,
			String Elt7Name, String Elt8Name,
			String Elt9Name, String Elt10Name,
			String Elt11Name, String Elt12Name,
			String Elt13Name, String Elt14Name,
			String Elt15Name, String Elt16Name){
		try{
			NodeList nodelist  = doc.getDocumentElement().getElementsByTagName(NodeName) ;

			if(nodelist != null && nodelist.getLength() > 0) {
				String EltArray [][]  = new String  [nodelist.getLength()][16]; 
				for (int i=0;i<nodelist.getLength();i++){
					Element el = (Element)nodelist.item(i);
					EltArray[i][0]  = el.getAttribute( Elt1Name);
					EltArray[i][1]  = el.getAttribute( Elt2Name);
					EltArray[i][2]  = el.getAttribute( Elt3Name);
					EltArray[i][3]  = el.getAttribute( Elt4Name);
					EltArray[i][4]  = el.getAttribute( Elt5Name);
					EltArray[i][5]  = el.getAttribute( Elt6Name);
					EltArray[i][6]  = el.getAttribute( Elt7Name);
					EltArray[i][7]  = el.getAttribute( Elt8Name);
					EltArray[i][8]  = el.getAttribute( Elt9Name);
					EltArray[i][9]  = el.getAttribute( Elt10Name);
					EltArray[i][10] = el.getAttribute( Elt11Name);
					EltArray[i][11] = el.getAttribute( Elt12Name);
					EltArray[i][12] = el.getAttribute( Elt13Name);
					EltArray[i][13] = el.getAttribute( Elt14Name);
					EltArray[i][14] = el.getAttribute( Elt15Name);
					EltArray[i][15] = el.getAttribute( Elt16Name);
				}
				return EltArray ;
			}
		} catch (Exception e) {
			System.out.println("DOM.Get15EltsArray : "+e.toString());
		}
		return null ;
	}
	// 
	// 
	@SuppressWarnings("unused")
	private String getTextValue(Element ele, String tagName) {
		String textVal = null;
		NodeList nl = ele.getElementsByTagName(tagName);
		if(nl != null && nl.getLength() > 0) {
			Element el = (Element)nl.item(0);
			textVal = el.getFirstChild().getNodeValue();
		}

		return textVal;
	}
}



